package com.example.s.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.bumptech.glide.Glide;
import com.example.s.Activity.search.SearchActivity;
import com.example.s.MainActivity;
import com.example.s.R;
import com.example.s.adapter.MyFragmentPagerAdapter;
import com.example.s.application.MyApplication;
import com.example.s.bean.User;
import com.example.s.fragment.main_child_fragment.DiscoveryFragment;
import com.example.s.fragment.main_child_fragment.FollowFragment;
import com.example.s.fragment.main_child_fragment.NearbyFragment;
import com.example.s.view.MyViewPager;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class MainFragment extends BaseFragment {

    private final String TAG = "MainFragment";
    private CircleImageView my;
    private TextView discovery;
    private TextView follow;
    private TextView nearby;
    private ImageView search;
    private MyViewPager vp;
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    private List<Fragment> mFragmentList = new ArrayList<>();
    private FollowFragment mFollowFragment;
    private DiscoveryFragment mDiscoveryFragment;
    private NearbyFragment mNearbyFragment;
    private ImageView chosePlace;
    private MainActivity.ToParent toParent;

    public void setToParent(MainActivity.ToParent toParent) {
        this.toParent = toParent;
    }


    @Override
    public int getLayoutId() {
        return R.layout.fragment_main;
    }

    @Override
    public void initView(View view) {
        my = (CircleImageView) findViewById(R.id.my);
        discovery = (TextView) findViewById(R.id.discovery);
        follow = (TextView) findViewById(R.id.follow);
        nearby = (TextView) findViewById(R.id.nearby);
        search = (ImageView) findViewById(R.id.search);
        vp = (MyViewPager) findViewById(R.id.vp);
        chosePlace = (ImageView) findViewById(R.id.chosePlace);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        User user = MyApplication.getInstance(A).getUser();
        if(user!=null)
        Glide.with(A).load(user.getHeadPhoto()).into(my);
        my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (toParent != null) {
                    toParent.passMsg();
                }
            }
        });
        search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SearchActivity.class));
            }
        });
        mFollowFragment = new FollowFragment();
        mDiscoveryFragment = new DiscoveryFragment();
        mNearbyFragment = new NearbyFragment();

        mFragmentList.add(mFollowFragment);
        mFragmentList.add(mDiscoveryFragment);
        mFragmentList.add(mNearbyFragment);
        vp.setAdapter(mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), mFragmentList));

        vp.setCurrentItem(1); //发现
        vp.setOffscreenPageLimit(3);


        final TextView[] textViews = {follow, discovery, nearby};
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Log.d(TAG, "onPageSelected: " + position);
                changeTab(textViews, position);
                if (position == 2) {
                    chosePlace.setVisibility(View.VISIBLE);
                } else {
                    chosePlace.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        for (int i = 0; i < textViews.length; i++) {
            final int finalI = i;
            textViews[finalI].setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    changeTab(textViews, finalI);
                    vp.setCurrentItem(finalI);
                }
            });
        }
    }


    private void changeTab(TextView[] textViews, int position) {
        for (TextView textView : textViews) {
            textView.setTextSize(14);
            textView.setTextColor(Color.parseColor("#8AFFFFFF"));
        }
        textViews[position].setTextColor(Color.parseColor("#ffffff"));
        textViews[position].setTextSize(20);
    }


}
